import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.GraphicsConfiguration;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.media.j3d.AmbientLight;
import javax.media.j3d.Appearance;
import javax.media.j3d.Background;
import javax.media.j3d.BoundingBox;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.DirectionalLight;
import javax.media.j3d.Material;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3d;
import javax.vecmath.Vector3f;

//import org.haptiklibrary.HaptikDevice;

import com.sun.j3d.utils.behaviors.keyboard.KeyNavigatorBehavior;
import com.sun.j3d.utils.geometry.Box;
import com.sun.j3d.utils.geometry.Cylinder;
import com.sun.j3d.utils.geometry.Sphere;
import com.sun.j3d.utils.image.TextureLoader;
import com.sun.j3d.utils.picking.behaviors.PickTranslateBehavior;
import com.sun.j3d.utils.picking.behaviors.PickZoomBehavior;
import com.sun.j3d.utils.universe.SimpleUniverse;
import com.sun.j3d.utils.universe.ViewingPlatform;





public class testObjects extends Applet {
        SimpleUniverse simpleU =null;
        Canvas3D canvas3D = null;
        communicationBridge communicate = new communicationBridge();
        TransformGroup puck = null;
        TransformGroup myHandle = null;
        TransformGroup othersHandle = null;
        Server tcpServer = null;
        Client connectionToPlayer = null;
        String ipOfOtherPlayer = "";
        protected BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0,
      0.0), 100.0);
        Vector3f puckVelocity;
//        private HaptikDevice device;

	public static void main( String[] args) throws IOException {
		
            int port = Integer.parseInt(args[0]);
            int playerID = Integer.parseInt(args[1]);
            final Server tcpServer = new Server(port);
            
            
            //final Client connectionToPlayer = new Client("127.0.0.1");
            final Client connectionToPlayer = new Client();
            tcpServer.start();
            final JFrame window = new JFrame("Air Hockey");
            final testObjects VRScene = new testObjects(tcpServer, connectionToPlayer,playerID);
            window.setContentPane(VRScene);
            window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JPanel bottom = new JPanel();
            bottom.setAlignmentX(1f);
            bottom.setLayout(new BoxLayout(bottom, BoxLayout.X_AXIS));
            final JFormattedTextField tf = new JFormattedTextField();
            tf.setValue("127.0.0.1:5441");
            tf.setColumns(10);

            JButton connectButton = new JButton("Connect");
            connectButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e)
                {
                    String[] temp;
                    //for some reason this connection starts to die
                    System.out.println("Connecting to: " + tf.getText());
                    //parse for port? not if you are 100% sure it will be on another computer, but for current experiments maybe good
                    temp=tf.getText().split(":");
                    connectionToPlayer.setServerToConnectTo(temp[0]);
                    connectionToPlayer.setServerPortToConnectTo(Integer.parseInt( temp[1] ));
                    connectionToPlayer.start();
                }
            });
            //Add action listener to button
            JButton exitButton = new JButton("exit");
            //Add action listener to button
            exitButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e)
                {
                    //Execute when button is pressed
                    System.exit(1);
                }
            });
            //add buttons to separate panel in frame
            bottom.add(connectButton);
            bottom.add(tf);
            //bottom.add(openButton);
            bottom.add(exitButton);
            window.getContentPane().add(bottom, BorderLayout.SOUTH);
            window.pack();              // Arrange the components.
            window.setSize(500, 500);
            window.setVisible(true);    // Make the window visible.
	}
        /*public void moveThePuck(Server tcpServer, Client connectionToPlayer ) throws IOException {
            communicate.updateCordForObject(tcpServer,puck,"puck");
            communicate.sendWorldInfo(connectionToPlayer, puck, myHandle, null);
        }
         */
        public void setIpOfOtherPlayer(String ipOfOtherPlayer) {
            this.ipOfOtherPlayer=ipOfOtherPlayer;
        }
	public testObjects(Server tcpServer, Client connectionToPlayer, int playerID) {
                this.tcpServer = tcpServer;
                this.connectionToPlayer = connectionToPlayer;
		setLayout(new BorderLayout());
		puckVelocity=new Vector3f(0.0f,0f,0.0f);
		GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
		canvas3D = new Canvas3D(config);
		add("Center", canvas3D);
		simpleU = new SimpleUniverse(canvas3D);
		BranchGroup scene = CreateSceneGraph(simpleU,canvas3D,playerID);
		scene.compile();
		simpleU.getViewingPlatform().setNominalViewingTransform();
		simpleU.addBranchGraph(scene);

                //set view
                ViewingPlatform vp = simpleU.getViewingPlatform();
                TransformGroup View_TransformGroup = vp.getMultiTransformGroup().getTransformGroup(0);
                
                if (playerID ==2) {
                    Transform3D View_Transform3D = new Transform3D();
                    Transform3D View_Rotation = new Transform3D();
                    View_Rotation.rotX(-Math.PI/8);
                    View_TransformGroup.getTransform(View_Transform3D);
                    View_Transform3D.mul(View_Rotation);
                    View_Transform3D.setTranslation(new Vector3f(0.0f,10.0f,37.0f));
                    View_TransformGroup.setTransform(View_Transform3D);   
                }
                if (playerID ==1) {
                    Transform3D View_Transform3D = new Transform3D();
                    Transform3D View_Rotation = new Transform3D();
                    View_Rotation.rotY(-Math.PI);
                    Transform3D View_Rotation2 = new Transform3D();
                    View_Rotation2.rotX(-Math.PI/8);
                    View_Rotation.mul(View_Rotation2);
                    View_TransformGroup.getTransform(View_Transform3D);
                    View_Transform3D.mul(View_Rotation);
                    View_Transform3D.setTranslation(new Vector3f(0.0f,10.0f,-37.0f));
                    View_TransformGroup.setTransform(View_Transform3D);
                }            
	}

	public BranchGroup  CreateSceneGraph(SimpleUniverse simpleU, Canvas3D canvas, int playerID) {
		 TransformGroup faceGroup;

//		 device = HaptikDevice.GetHaptikDevice(HaptikDevice.HAPTIK_DEFAULT_DEVICE);
//   	  	 device.Init();
//   	  	 device.Start();
		  /** Transform for the right cube */
		 TransformGroup rightWallGroup;
		 TransformGroup rightTopWallGroup;
		 TransformGroup rightBottomWallGroup;
		 TransformGroup leftWallGroup;
		 TransformGroup leftTopWallGroup;
		 TransformGroup leftBottomWallGroup;
		 TransformGroup topGoalGroup;
		 TransformGroup bottomGoalGroup;
		 TransformGroup puckGroup;
		  /**
		   * Transform for the movable cube. This has read, write and pick reporting
		   * capabilities enabled.
		   */
		 TransformGroup Handle_top_1_Group;
		 TransformGroup handle_1_Group;
		 
		 TransformGroup Handle_top_2_Group;
		 TransformGroup handle_2_Group;

		  /** The left static cube. */
		 Box face;

		  /** The right static cube. */
		 Box rightWall;
		 Box rightTopWall;
		 Box rightBottomWall;
		 Box leftWall;
		 Box leftTopWall;
		 Box leftBottomWall;
		 Box topGoal;
		 Box bottomGoal;
		 Cylinder puck;

		  /** The movable cube that will collide with the other two cubes */
		 Cylinder Handle_base;
		 Cylinder Handle_top;
		 Cylinder Handle_base_2;
		 Cylinder Handle_top_2;
	    //First create a different appearance for each cube
	    Appearance app1 = new Appearance(); //for face
	    Appearance app2 = new Appearance(); //for right
	    Appearance app3 = new Appearance(); //for handle
	    Appearance app4 = new Appearance(); //for left
	    Appearance app5 = new Appearance(); //for goal
	    Appearance app6 = new Appearance(); //for puck
	    Color3f ambientColour1 = new Color3f(1.0f, 0.0f, 0.0f); //for face
	    Color3f ambientColour2 = new Color3f(1.0f, 1.0f, 0.0f); //for right
	    Color3f ambientColour3 = new Color3f(0.0f, 0.0f, 1.0f); //for handle
	    Color3f ambientColour4 = new Color3f(0.0f, 1.0f, 1.0f); //for left
	    Color3f ambientColour5 = new Color3f(0.0f, 0.0f, 0.0f); //for goal
	    Color3f ambientColour6 = new Color3f(1.0f, 1.0f, 1.0f); //for puck
	    Color3f emissiveColour = new Color3f(0.0f, 0.0f, 0.0f);
	    Color3f specularColour = new Color3f(1.0f, 1.0f, 1.0f);
	    Color3f diffuseColour1 = new Color3f(1.0f, 0.0f, 0.0f);
	    Color3f diffuseColour2 = new Color3f(1.0f, 1.0f, 0.0f);
	    Color3f diffuseColour3 = new Color3f(0.0f, 0.0f, 0.7f);
	    Color3f diffuseColour4 = new Color3f(0.0f, 0.7f, 0.7f);
	    Color3f diffuseColour5 = new Color3f(0.0f, 0.0f, 0.0f);
	    Color3f diffuseColour6 = new Color3f(1.0f, 1.0f, 1.0f);
	    float shininess = 20.0f;
	    app1.setMaterial(new Material(ambientColour1, emissiveColour,
	        diffuseColour1, specularColour, shininess));
	    app2.setMaterial(new Material(ambientColour2, emissiveColour,
	        diffuseColour2, specularColour, shininess));
	    app3.setMaterial(new Material(ambientColour3, emissiveColour,
	        diffuseColour3, specularColour, shininess));
	    app4.setMaterial(new Material(ambientColour4, emissiveColour,
	            diffuseColour4, specularColour, shininess));
	    app5.setMaterial(new Material(ambientColour5, emissiveColour,
	            diffuseColour5, specularColour, shininess));
	    app6.setMaterial(new Material(ambientColour6, emissiveColour,
	            diffuseColour6, specularColour, shininess));

	    //Create the three cubes
	    face = new Box(5.0f, 1.0f,15.0f,app1);
	    rightWall = new Box(0.25f, 0.5f,15.0f, app2);
	    rightTopWall = new Box(1.25f, 0.5f,0.25f, app2);
	    rightBottomWall = new Box(1.25f, 0.5f,0.25f, app4);
	    leftWall = new Box(0.25f, 0.5f,15.0f, app4);
	    leftTopWall = new Box(1.25f, 0.5f,0.25f, app4);
	    leftBottomWall = new Box(1.25f, 0.5f,0.25f, app2);
	    //Handle_base = new Box(0.4f, 0.2f,0.4f, app3);
	    Handle_base = new Cylinder(0.4f,0.2f, app3);
	    Handle_top = new Cylinder(0.1f,0.4f, app3);
	    Handle_base_2 = new Cylinder(0.4f,0.2f, app2);
	    Handle_top_2 = new Cylinder(0.1f,0.4f, app2);
	    topGoal = new Box(2.0f, 0.5f,0.125f, app5);
	    bottomGoal = new Box(2.0f, 0.5f,0.125f, app5);
	    puck= new Cylinder(0.4f,0.2f,app6);
	    
	    Transform3D handletopTrans = new Transform3D();
	    handletopTrans.set(new Vector3d(0.0, 0.3, 0));
	    Handle_top_1_Group=new TransformGroup(handletopTrans);
	    Handle_top_1_Group.addChild(Handle_top);
	    Handle_top_2_Group=new TransformGroup(handletopTrans);
	    Handle_top_2_Group.addChild(Handle_top_2);

	    
	   

	    //Create the content branch and add the lights
	    BranchGroup contentBranch = new BranchGroup();
	    addLights(contentBranch);

	    //Set up the transform to position the left cube
	    Transform3D faceGroupXfm = new Transform3D();
	    faceGroupXfm.set(new Vector3d(0.0, 0.0, 0.0));
	    faceGroup = new TransformGroup(faceGroupXfm);

	    //Set up the transform to position the right cube
	    Transform3D rightWallTrans = new Transform3D();
	    rightWallTrans.set(new Vector3d(4.75, 1.5, 0.0));
	    rightWallGroup = new TransformGroup(rightWallTrans);
	    
	    //Set up the transform to position the right top wall
	    Transform3D rightTopWallTrans = new Transform3D();
	    rightTopWallTrans.set(new Vector3d(3.25, 1.5, -14.75));
	    rightTopWallGroup = new TransformGroup(rightTopWallTrans);
	    
	  //Set up the transform to position the right bottom wall
	    Transform3D rightBottomWallTrans = new Transform3D();
	    rightBottomWallTrans.set(new Vector3d(3.25, 1.5, 14.75));
	    rightBottomWallGroup = new TransformGroup(rightBottomWallTrans);
	    
	  //Set up the transform to position the left wall
	    Transform3D leftWallTrans = new Transform3D();
	    leftWallTrans.set(new Vector3d(-4.75, 1.5, 0.0));
	    leftWallGroup = new TransformGroup(leftWallTrans);
	    
	  //Set up the transform to position the left top wall
	    Transform3D leftTopWallTrans = new Transform3D();
	    leftTopWallTrans.set(new Vector3d(-3.25, 1.5, -14.75));
	    leftTopWallGroup = new TransformGroup(leftTopWallTrans);
	    
	    //Set up the transform to position the left bottom wall
	    Transform3D leftBottomWallTrans = new Transform3D();
	    leftBottomWallTrans.set(new Vector3d(-3.25, 1.5, 14.75));
	    leftBottomWallGroup = new TransformGroup(leftBottomWallTrans);
	    
	    //Set up the transform to position the left bottom wall
	    Transform3D topGoalTrans = new Transform3D();
	    topGoalTrans.set(new Vector3d(0.0, 1.5, -14.875));
	    topGoalGroup = new TransformGroup(topGoalTrans);
	    
	    //Set up the transform to position the left bottom wall
	    Transform3D bottomGoalTrans = new Transform3D();
	    bottomGoalTrans.set(new Vector3d(0.0, 1.5, 14.875));
	    bottomGoalGroup = new TransformGroup(bottomGoalTrans);
	    
	    faceGroup.setCollisionBounds(new BoundingBox(new Point3d(-5.0f, -1.0f,-15.0f),new Point3d(5.0f, 1.0f,15.0f)));
	    rightWallGroup.setCollisionBounds(new BoundingBox(new Point3d(-0.8,-0.8,-0.8),new Point3d(0.8,0.8,0.8)));
	    
	    

	    //Create the movable cube's transform with a scale and
	    //a translation. Set up the
	    //capabilities so it can be moved by the behaviour
	    Transform3D moveXfm = new Transform3D();
	    moveXfm.set(1.0, new Vector3d(0.0, 3.0, -8.0));
	    handle_1_Group = new TransformGroup(moveXfm);
	    handle_1_Group.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
	    handle_1_Group.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
	    handle_1_Group.setCapability(TransformGroup.ENABLE_PICK_REPORTING);
	    
	    Transform3D moveXfm2 = new Transform3D();
	    moveXfm2.set(1.0, new Vector3d(0.0, 3.0, 8.0));
	    handle_2_Group = new TransformGroup(moveXfm2);
	    handle_2_Group.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
	    handle_2_Group.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
	    handle_2_Group.setCapability(TransformGroup.ENABLE_PICK_REPORTING);
	    
	    Transform3D puckXfm = new Transform3D();
	    puckXfm.set(new Vector3d(0.0, 1.1, 10.0));
	    puckGroup = new TransformGroup(puckXfm);
	    puckGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
	    puckGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
	    puckGroup.setCapability(TransformGroup.ENABLE_PICK_REPORTING);
	    
	    /*
	 
	    if(playerID==1){
	    	RefreshBehavior sendMyHandle = new RefreshBehavior(puckGroup, handle_1_Group,1,connectionToPlayer,communicate,puckVelocity);
        	sendMyHandle.setSchedulingBounds(new BoundingSphere());
        	handle_1_Group.addChild(sendMyHandle);
        	
        	//get other player's handle cords
        	RefreshBehavior refreshPuck = new RefreshBehavior(handle_2_Group,0,tcpServer,communicate, playerID);
            refreshPuck.setSchedulingBounds(new BoundingSphere());
            handle_2_Group.addChild(refreshPuck);
            
            //read puck data when it passes this player's limit
            RefreshBehavior updatePuck = new RefreshBehavior(puckGroup,2,tcpServer,communicate, playerID,puckVelocity);
            updatePuck.setSchedulingBounds(new BoundingSphere());
            puckGroup.addChild(updatePuck);
	    }
	    
	    if(playerID==2){
	    	RefreshBehavior sendMyHandle = new RefreshBehavior(puckGroup, handle_2_Group,1,connectionToPlayer,communicate,puckVelocity);
        	sendMyHandle.setSchedulingBounds(new BoundingSphere());
        	handle_2_Group.addChild(sendMyHandle);
        	
        	//get other player's handle cords
        	RefreshBehavior refreshPuck = new RefreshBehavior(handle_1_Group,0,tcpServer,communicate, playerID);
            refreshPuck.setSchedulingBounds(new BoundingSphere());
            handle_1_Group.addChild(refreshPuck);
            
            //read puck data when it passes this player's limit
            RefreshBehavior updatePuck = new RefreshBehavior(puckGroup,2,tcpServer,communicate, playerID,puckVelocity);
            updatePuck.setSchedulingBounds(new BoundingSphere());
            puckGroup.addChild(updatePuck);
            
	    }
	    
	    
	 */   
	    
	    
	    
	    
	    
	    
	    
	    
	    

	    
	    Handle_base.setBounds(new BoundingBox(new Point3d(-0.8,-0.8,-0.8),new Point3d(0.8,0.8,0.8)));
	    handle_1_Group.setCollisionBounds(new BoundingBox(new Point3d(-0.8,-0.8,-0.8),new Point3d(0.8,0.8,0.8)));
	    //Create the behaviour to move the movable cube
	    PickTranslateBehavior pickTranslate = new PickTranslateBehavior(
	        contentBranch, canvas3D, Handle_base.getBounds());
	    contentBranch.addChild(pickTranslate);
	    PickZoomBehavior mypickZoom = new PickZoomBehavior(
	            contentBranch, canvas3D, Handle_base.getBounds());
	        contentBranch.addChild(mypickZoom);
	     
	        PickTranslateBehavior pickTranslate2 = new PickTranslateBehavior(
	                contentBranch, canvas3D, puck.getBounds());
	            contentBranch.addChild(pickTranslate2);
	            PickZoomBehavior mypickZoom2 = new PickZoomBehavior(
	                    contentBranch, canvas3D, puck.getBounds());
	                contentBranch.addChild(mypickZoom2);
	                
	  //Define some user data so that we can print meaningful messages
	    handle_1_Group.setUserData(new String("player_1"));
	    handle_2_Group.setUserData(new String("player_2"));

	    //Create and add the two colision detectors
	    if(playerID==1)
	    {
	    	//HandleCollisionDetector handleCollision = new HandleCollisionDetector(handle_1_Group, puckGroup,device,puckVelocity,playerID);
	    	//contentBranch.addChild(handleCollision);
	    	PuckCollisionDetector puckCollision = new PuckCollisionDetector(puckGroup, handle_1_Group, puckVelocity, playerID);
	    	contentBranch.addChild(puckCollision);
	    	
	    	
	    	RefreshBehavior sendMyHandle = new RefreshBehavior(puckGroup, handle_1_Group,1,connectionToPlayer,communicate,puckVelocity);
        	sendMyHandle.setSchedulingBounds(new BoundingSphere());
        	handle_1_Group.addChild(sendMyHandle);
        	
        	//get other player's handle cords
        	RefreshBehavior refreshPuck = new RefreshBehavior(handle_2_Group,0,tcpServer,communicate, playerID);
            refreshPuck.setSchedulingBounds(new BoundingSphere());
            handle_2_Group.addChild(refreshPuck);
            
            //read puck data when it passes this player's limit
            RefreshBehavior updatePuck = new RefreshBehavior(puckGroup,2,tcpServer,communicate, playerID,puckVelocity);
            updatePuck.setSchedulingBounds(new BoundingSphere());
            puckGroup.addChild(updatePuck);
	    	
	    	
	    }
	    //
	    if(playerID==2){
	    	//HandleCollisionDetector handleCollision2 = new HandleCollisionDetector(handle_2_Group, puckGroup,device,puckVelocity,playerID);
	    	//contentBranch.addChild(handleCollision2);
	    	PuckCollisionDetector puckCollision = new PuckCollisionDetector(puckGroup, handle_2_Group, puckVelocity, playerID);
	    	contentBranch.addChild(puckCollision);
	    	
	    	
	    	//System.out.println( puckCollision.V);
	    	
	    	RefreshBehavior sendMyHandle = new RefreshBehavior(puckGroup, handle_2_Group,1,connectionToPlayer,communicate,puckVelocity);
        	sendMyHandle.setSchedulingBounds(new BoundingSphere());
        	handle_2_Group.addChild(sendMyHandle);
        	
        	//get other player's handle cords
        	RefreshBehavior refreshPuck = new RefreshBehavior(handle_1_Group,0,tcpServer,communicate, playerID);
            refreshPuck.setSchedulingBounds(new BoundingSphere());
            handle_1_Group.addChild(refreshPuck);
            
            //read puck data when it passes this player's limit
            RefreshBehavior updatePuck = new RefreshBehavior(puckGroup,2,tcpServer,communicate, playerID,puckVelocity);
            updatePuck.setSchedulingBounds(new BoundingSphere());
            puckGroup.addChild(updatePuck);
	    }
	    

	    //
	    
	    

	    
	    //Set up the scene graph
	    contentBranch.addChild(handle_1_Group);
	    contentBranch.addChild(handle_2_Group);
	    contentBranch.addChild(faceGroup);
	    contentBranch.addChild(rightWallGroup);
	    contentBranch.addChild(rightTopWallGroup);
	    contentBranch.addChild(rightBottomWallGroup);
	    contentBranch.addChild(leftWallGroup);
	    contentBranch.addChild(leftTopWallGroup);
	    contentBranch.addChild(leftBottomWallGroup);
	    contentBranch.addChild(topGoalGroup);
	    contentBranch.addChild(bottomGoalGroup);
	    contentBranch.addChild(puckGroup);
	    handle_1_Group.addChild(Handle_base);
	    handle_1_Group.addChild(Handle_top_1_Group);
	    handle_2_Group.addChild(Handle_base_2);
	    handle_2_Group.addChild(Handle_top_2_Group);
	    faceGroup.addChild(face);
	    rightWallGroup.addChild(rightWall);
	    rightTopWallGroup.addChild(rightTopWall);
	    rightBottomWallGroup.addChild(rightBottomWall);
	    leftWallGroup.addChild(leftWall);
	    leftTopWallGroup.addChild(leftTopWall);
	    leftBottomWallGroup.addChild(leftBottomWall);
	    bottomGoalGroup.addChild(bottomGoal);
	    topGoalGroup.addChild(topGoal);
	    puckGroup.addChild(puck);
	    

	    
	    
	    
	    //key navigation
		TransformGroup newTransform=simpleU.getViewingPlatform().getViewPlatformTransform();
	    KeyNavigatorBehavior keyNav=new KeyNavigatorBehavior(newTransform);
		keyNav.setSchedulingBounds(bounds);
		TransformGroup objTrans = new TransformGroup();
		objTrans.addChild(keyNav);
		contentBranch.addChild(objTrans);
		
		//set backGround    
	    Background bg = new Background();
	    bg.setApplicationBounds(bounds);
	    BranchGroup backgeo= new BranchGroup();
	    Sphere sphereObj= new Sphere(1.1f, Sphere.GENERATE_NORMALS
	    		|Sphere.GENERATE_NORMALS_INWARD
				|Sphere.GENERATE_TEXTURE_COORDS, 45);
	    Appearance backGroundapp= sphereObj.getAppearance();
	    backgeo.addChild(sphereObj);
	    bg.setGeometry(backgeo);
	   
	    TextureLoader back = new TextureLoader("supernova.jpg",null);
	    if( back !=null){
	    	backGroundapp.setTexture(back.getTexture());
	    }
	    contentBranch.addChild(bg);
	       

	    return contentBranch;

  }


          protected void addLights(BranchGroup b) {
    Color3f ambLightColour = new Color3f(0.5f, 0.5f, 0.5f);
    AmbientLight ambLight = new AmbientLight(ambLightColour);
    ambLight.setInfluencingBounds(bounds);
    Color3f dirLightColour = new Color3f(1.0f, 1.0f, 1.0f);
    Vector3f dirLightDir = new Vector3f(-1.0f, -1.0f, -1.0f);
    DirectionalLight dirLight = new DirectionalLight(dirLightColour,
        dirLightDir);
    dirLight.setInfluencingBounds(bounds);
    b.addChild(ambLight);
    b.addChild(dirLight);
  }
}

